package com.d3works.apps.supplychain.service;

import com.d3works.apps.account.db.AccountingSituation;
import com.d3works.apps.account.db.repo.AccountingSituationRepository;
import com.d3works.apps.account.service.AccountCustomerServiceImpl;
import com.d3works.apps.account.service.AccountingSituationInitService;
import com.d3works.apps.account.service.AccountingSituationService;
import com.d3works.apps.base.service.app.AppBaseService;
import com.d3works.exception.D3worksException;
import com.google.inject.Inject;
import com.google.inject.persist.Transactional;

public class AccountCustomerServiceSupplyChain extends AccountCustomerServiceImpl {

  @Inject
  public AccountCustomerServiceSupplyChain(
      AccountingSituationService accountingSituationService,
      AccountingSituationInitService accountingSituationInitService,
      AccountingSituationRepository accSituationRepo,
      AppBaseService appBaseService) {
    super(
        accountingSituationService,
        accountingSituationInitService,
        accSituationRepo,
        appBaseService);
  }

  @Override
  @Transactional(rollbackOn = {Exception.class})
  public AccountingSituation updateAccountingSituationCustomerAccount(
      AccountingSituation accountingSituation,
      boolean updateCustAccount,
      boolean updateDueCustAccount,
      boolean updateDueDebtRecoveryCustAccount)
      throws D3worksException {

    accountingSituation =
        super.updateAccountingSituationCustomerAccount(
            accountingSituation,
            updateCustAccount,
            updateDueCustAccount,
            updateDueDebtRecoveryCustAccount);

    if (updateCustAccount && appBaseService.isApp("supplychain")) {
      accountingSituationService.updateCustomerCredit(accountingSituation.getPartner());
    }

    return accountingSituation;
  }
}
